import axios from './request'

export const host = 'http://localhost:3001'

export const uploadPhoto = (data = {}) => {
    return axios.post(`${host}/api/photo/upload`, data, { headers: { 'Content-Type': 'multipart/form-data' } })
}

export const removePhoto = (data = {}) => {
    return axios.post(`${host}/api/photo/delete`, data)
}

export const publishBanner = (data = {}) => {
    return axios.get(`${host}/api/banner/add`, { params: data })
}

export const listBanner = () => {
    return axios.get(`${host}/api/banner/list`)
}

export const searchBanner = (data = {}) => {
    return axios.get(`${host}/api/banner/search`, { params: data })
}

export const removeBanner = (data = {}) => {
    return axios.get(`${host}/api/banner/remove`, { params: data })
}

export const modifyBanner = (data = {}) => {
    return axios.get(`${host}/api/banner/modify`, { params: data })
}

export const addCate = (data = {}) => {
    return axios.get(`${host}/api/cate/add`, { params: data })
}

export const addCateBatch = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    if (!token) {
        console.warn('AUTO_TOKEN不存在，请先登录')
    }
    return axios.post(`${host}/api/cate/addBatch`, data, {
        headers: {
            token: token || ''
        }
    })
}

export const listCate = (data = {}) => {
    return axios.get(`${host}/api/cate/list`, { params: data })
}

export const searchCate = (data = {}) => {
    return axios.get(`${host}/api/cate/search`, { params: data })
}

export const removeCate = (data = {}) => {
    return axios.get(`${host}/api/cate/remove`, { params: data })
}

// 删除一级分类时会同时删除所有二级分类
export const removeCateByParentCate = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/cate/removeByParentCate`, data, {
        headers: {
            token: token || ''
        }
    })
}

export const modifyCate = (data = {}) => {
    return axios.get(`${host}/api/cate/modify`, { params: data })
}

export const itemsCate = (data = {}) => {
    return axios.get(`${host}/api/cate/items`, { params: data })
}

export const isLogined = (data = {}) => {
    return axios.post(`${host}/api/user/isLogined`, data)
}

export const adminLogin = (data = {}) => {
    return axios.post(`${host}/api/admin/login`, data)
}

export const userRegister = (data = {}) => {
    return axios.post(`${host}/api/user/register`, data)
}

export const userLogin = (data = {}) => {
    return axios.post(`${host}/api/user/login`, data)
}

export const publishArticle = (data = {}) => {
    return axios.post(`${host}/api/article/publish`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const itemsArticle = (data = {}) => {
    return axios.post(`${host}/api/article/items`, data)
}

export const searchArticle = (data = {}) => {
    return axios.post(`${host}/api/article/search`, data)
}

export const deleteArticle = (data = {}) => {
    return axios.post(`${host}/api/article/delete`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

// 仅管理员可用
export const itemsUser = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/user/items`, { ...data, token }, {
        headers: { token }
    })
}

export const deleteUser = (data = {}) => {
    return axios.post(`${host}/api/user/delete`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const getStatistics = (data = {}) => {
    return axios.post(`${host}/api/statistics/get`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const getArticleDetail = (data = {}) => {
    return axios.post(`${host}/api/article/detail`, data)
}

export const modifyArticle = (data = {}) => {
    return axios.post(`${host}/api/article/modify`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const itemsOperationLog = (data = {}) => {
    return axios.post(`${host}/api/operationLog/items`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const deleteOperationLog = (data = {}) => {
    return axios.post(`${host}/api/operationLog/delete`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const muteUser = (data = {}) => {
    return axios.post(`${host}/api/user/mute`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const banUser = (data = {}) => {
    return axios.post(`${host}/api/user/ban`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const unmuteUser = (data = {}) => {
    return axios.post(`${host}/api/user/unmute`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const unbanUser = (data = {}) => {
    return axios.post(`${host}/api/user/unban`, data, { headers: { token: localStorage.getItem("AUTO_TOKEN") } })
}

export const getCurrentUserDetail = () => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/user/detail2`, {}, { headers: { token } })
}

export const editCurrentUser = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/user/edit`, data, { headers: { token } })
}

// 管理员专用
export const itemsComment = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/comment/admin/list`, data, { headers: { token } })
}

export const searchComment = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/comment/admin/search`, data, { headers: { token } })
}

export const hideComment = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/comment/admin/hide`, data, { headers: { token } })
}

export const restoreComment = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/comment/admin/restore`, data, { headers: { token } })
}

export const deleteComment = (data = {}) => {
    const token = localStorage.getItem("AUTO_TOKEN")
    return axios.post(`${host}/api/comment/admin/delete`, data, { headers: { token } })
}